﻿using System;
using System.Collections.Generic;
using System.Linq;
using iTextSharp.text.pdf;

namespace Web.Helpers
{
    public class OfficeHelper
    {
        #region Constants
        public static class FontStyle
        {
            public const int Bold = -1;
            public const int Italic = -1;
            //public const WdUnderline Underline = WdUnderline.wdUnderlineNone;
        }
        #endregion

        //public class WordDocument
        //{
        //    public Application Application { get; set; }
        //    public Document Document { get; set; }

        //    public Table GetTable(int index)
        //    {
        //        return this.Document.Tables[index];
        //    }
        //}

        //public static WordDocument GetDocument(string filename)
        //{
        //    WordDocument wordDocument = null;
        //    try
        //    {
        //        var app = new Microsoft.Office.Interop.Word.Application();
        //        var doc = app.Documents.OpenNoRepairDialog(FileName: filename, ConfirmConversions: false, ReadOnly: true, AddToRecentFiles: false, NoEncodingDialog: true);
        //        wordDocument = new WordDocument() {Application = app, Document = doc};
        //    }
        //    catch (Exception)
        //    {
        //        throw;
        //    }
        //    return wordDocument;
        //}

        //public class TextInfo
        //{
        //    public class CharacterInfo
        //    {
        //        #region Properties
        //        public bool Bold { get; set; }
        //        public bool Italic { get; set; }
        //        public bool Underline { get; set; }
        //        public string Character { get; set; }
        //        #endregion

        //        #region Constructors
        //        public CharacterInfo()
        //        {
                    
        //        }
        //        public CharacterInfo(string character, bool bold = false, bool italic = false, bool underline = false)
        //        {
        //            this.Character = character;
        //            this.Bold = bold;
        //            this.Italic = italic;
        //            this.Underline = underline;
        //        }
        //        #endregion
        //    }

        //    #region Properties
        //    public List<CharacterInfo> Characters { get; set; }
        //    public string Text { get; set; }
        //    #endregion

        //    #region Constructors
        //    public TextInfo(Range range)
        //    {
        //        this.Characters = this.GetTextWithFormat(range);
                
        //        var allDialogueCharacters = this.Characters.Select(x => x.Character).ToArray();
        //        var words = String.Join("", allDialogueCharacters);

        //        this.Text = ParseRawText(words);
        //    }
        //    #endregion

        //    #region Private Methods
        //    private List<CharacterInfo> GetTextWithFormat(Range text)
        //    {
        //        var characters = new List<CharacterInfo>();
        //        foreach (Range character in text.Characters)
        //        {
        //            if ( character.Text == "\r\a" )
        //                continue;
        //            var characterInfo = new CharacterInfo
        //            {
        //                Character   = character.Text,
        //                Bold        = character.Bold == FontStyle.Bold,
        //                Italic      = character.Italic == FontStyle.Italic,
        //                Underline   = character.Underline != FontStyle.Underline
        //            };
        //            characters.Add( characterInfo );
        //        }
        //        return characters;
        //    }
        //    #endregion
        //}

        //public static int GetNumberOfPages(WordDocument document)
        //{
        //    return document.Document.ComputeStatistics(WdStatistic.wdStatisticPages, Type.Missing);            
        //}
        //public static int GetPageNumber(Range range)
        //{
        //    return (int)range.Information[WdInformation.wdActiveEndPageNumber];
        //}

        //public static Tables GetAllTables(Document document)
        //{
        //    return document.Tables;
        //}
        //public static Table GetTable(Document document, int tableIndex)
        //{
        //     return document.Tables[tableIndex];
        //}

        //public static Rows GetRows(Document document, int tableIndex)
        //{
        //    return document.Tables[tableIndex].Rows;
        //}

        //public static string GetCellData(Cell cell)
        //{
        //    return ParseRawText( cell.Range.Text );
        //}
        //public static string GetCellData(Row row, int columnIndex)
        //{
        //    string value = null;
        //    try
        //    {
        //        value = GetCellData( row.Cells[columnIndex] );
        //    }
        //    catch (Exception)
        //    {
        //        throw;
        //    }
        //    return value;
        //}
        //public static string GetCellData(Table table, int rowIndex, int columnIndex)
        //{
        //    string value = null;
        //    try
        //    {
        //        value = GetCellData( table.Rows[rowIndex].Cells[columnIndex] );
        //    }
        //    catch (Exception)
        //    {
        //        throw;
        //    }
        //    return value;
        //}

        //private static string ParseRawText(string rawText)
        //{
        //    return rawText.Replace("\r", String.Empty).Replace("\a", String.Empty).Trim();
        //}

        //public static void Dispose(WordDocument worDoc)
        //{
        //    try
        //    {
        //        if ( worDoc.IsNull() || worDoc.Document.IsNull() || worDoc.Application.IsNull() )
        //            return;                
        //        worDoc.Document.Close(false);
        //        worDoc.Application.Quit();
        //        System.Runtime.InteropServices.Marshal.ReleaseComObject(worDoc.Document);            
        //        System.Runtime.InteropServices.Marshal.ReleaseComObject(worDoc.Application);
        //        worDoc.Document = null;
        //        GC.Collect(); //force final cleanup
        //    }
        //    catch (Exception ex)
        //    {
        //        ////Ensure file is not read-only
        //        //File.SetAttributes(fileName, FileAttributes.Normal);
        //        //throw;
        //    }
        //}

    }
}